﻿using System.Windows;
using IntelligentStorWpfApp.Model;

namespace IntelligentStorWpfApp.View
{
    /// <summary>
    /// AddProduct.xaml 的交互逻辑
    /// </summary>
    public partial class AddProduct : Window
    {
        public AddProduct()
        {
            InitializeComponent();
        }
        public ProductInfo product { get; set; }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            txtProductId.Text = "";
            txtProductNum.Text = "";
            txtStatus.Text ="";
            txtDesc.Text = "";
            product = new ProductInfo();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            int productNum = 0;
            if(string.IsNullOrEmpty(txtProductId.Text.Trim()))
            {
                MessageBox.Show("产品编号不能为空!");
                txtProductId.Focus();
                return;
            }
            if(string.IsNullOrEmpty(txtProductNum.Text.Trim()))
            {
                MessageBox.Show("产品数量不能为空!");
                txtProductNum.Focus();
                return;
            }
            if(string.IsNullOrEmpty(txtStatus.Text.Trim()))
            {
                MessageBox.Show("产品状态不能为空!");
                txtStatus.Focus();
                return;
            }
            if(int.TryParse(txtProductNum.Text.Trim(),out productNum))
            {
                if(productNum>0)
                {
                    product.ProductNum = productNum;
                }
                else
                {
                    MessageBox.Show("产品数量必须大于0!");
                    txtProductNum.Focus();
                    return;
                }
            }
            else
            {
                MessageBox.Show("数量必须是数字!");
                txtProductNum.Focus();
                return;
            }
            product.ProductId = txtProductId.Text.Trim();
            product.Status = txtStatus.Text.Trim();
            product.Description = txtDesc.Text.Trim();
            product.Owner = App.user.UserName;
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
